package cn.kailangtu.dataspot.activify;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import cn.kailangtu.commonevents.eventEntity.KeyboardTimeActionEvent;
import cn.kailangtu.commonevents.eventEntity.KeyboardTimeCloseEvent;
import cn.kailangtu.commonevents.eventEntity.KeyboardTimeResultEvent;
import cn.kailangtu.commontools.AdbUtil;
import cn.kailangtu.commontools.TimeUtils;
import cn.kailangtu.commontools.ToastUtils;
import cn.kailangtu.dataspot.data.consts.KeyboardAction;
import cn.kailangtu.dataspot.data.context.KeyboardTimeContext;
import cn.kailangtu.dataspot.databinding.FragmentKeyboardTimeBinding;

public class KeyboardTimeFragment extends KeyboardBaseFragment {

    private FragmentKeyboardTimeBinding binding;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentKeyboardTimeBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initListener();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    private void initListener(){

        binding.btnTimeCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new KeyboardTimeResultEvent(false,null));
            }
        });
        binding.btnTimeSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.YEAR,KeyboardTimeContext.year);
                calendar.set(Calendar.MONTH,KeyboardTimeContext.month -1);
                calendar.set(Calendar.DAY_OF_MONTH,KeyboardTimeContext.day);
                calendar.set(Calendar.HOUR_OF_DAY,KeyboardTimeContext.hour);
                calendar.set(Calendar.MINUTE,KeyboardTimeContext.minute);
                calendar.set(Calendar.SECOND,KeyboardTimeContext.second);
                EventBus.getDefault().post(new KeyboardTimeResultEvent(true,calendar));
            }
        });

        binding.tvKeyboardTimeYear.setOnClickListener(timeOnClickListener);
        binding.tvKeyboardTimeMonth.setOnClickListener(timeOnClickListener);
        binding.tvKeyboardTimeDay.setOnClickListener(timeOnClickListener);
        binding.tvKeyboardTimeHour.setOnClickListener(timeOnClickListener);
        binding.tvKeyboardTimeMinute.setOnClickListener(timeOnClickListener);
        binding.tvKeyboardTimeSecond.setOnClickListener(timeOnClickListener);
    }

    View.OnClickListener timeOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            KeyboardTimeContext.setSelectedView((TextView)v);
            KeyboardTimeContext.entry();
            resetData();
        }
    };

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(KeyboardTimeResultEvent event){
        if(event.isSave())
        {


            try {
                //TimeUtils.setTime(getContext(), event.getCalendar());
                //TimeUtils.setSystemTime(getContext(),event.getCalendar().getTimeInMillis());

                SimpleDateFormat format =  new SimpleDateFormat("MMddHHmmyyyy.ss", Locale.getDefault());
                String timeStr = format.format(event.getCalendar().getTimeInMillis());
                Log.d("TIMETIMETIME",timeStr);
                AdbUtil.run("date '"+timeStr+"'");
            }
            catch(Exception e)
            {
                e.printStackTrace();
                ToastUtils.ShowInCenter(getContext(),"权限不够");
            }


        }
        EventBus.getDefault().post(new KeyboardTimeCloseEvent());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(KeyboardTimeActionEvent event){
        keyboard(event.getActionValue());
    }

    private void keyboard(int action ){
        switch (action) {
            case KeyboardAction.OPEN:
               // binding.tvKeyboardConfirmMsg.setText(KeyboardConfirmContext.tipMsg);
                Calendar calendar = Calendar.getInstance();
                KeyboardTimeContext.year = calendar.get(Calendar.YEAR);
                KeyboardTimeContext.month = calendar.get(Calendar.MONTH)+1;
                KeyboardTimeContext.day = calendar.get(Calendar.DAY_OF_MONTH);
                KeyboardTimeContext.hour = calendar.get(Calendar.HOUR_OF_DAY);
                KeyboardTimeContext.minute = calendar.get(Calendar.MINUTE);
                KeyboardTimeContext.second = calendar.get(Calendar.SECOND);
                Log.e("TTTTTTTT","Y:"+KeyboardTimeContext.year);
                Log.e("TTTTTTTT","Y:"+KeyboardTimeContext.month);
                Log.e("TTTTTTTT","Y:"+KeyboardTimeContext.day);
                Log.e("TTTTTTTT","Y:"+KeyboardTimeContext.hour);
                Log.e("TTTTTTTT","Y:"+KeyboardTimeContext.minute);
                Log.e("TTTTTTTT","Y:"+KeyboardTimeContext.second);


                resetData();
                KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeYear);
                break;
            case KeyboardAction.ENTRY:
                KeyboardTimeContext.entry();
               // binding.tvKeyboardResult.setText(KeyboardContext.result);
                resetData();
                break;
            case KeyboardAction.LEFT:
                if(binding.tvKeyboardTimeYear.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.btnTimeSave);
                }
                else  if(binding.btnTimeSave.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.btnTimeCancel);
                }
                else  if(binding.btnTimeCancel.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeSecond);
                }
                else  if(binding.tvKeyboardTimeSecond.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeMinute);
                }
                else  if(binding.tvKeyboardTimeMinute.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeHour);
                }
                else  if(binding.tvKeyboardTimeHour.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeDay);
                }
                else  if(binding.tvKeyboardTimeDay.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeMonth);
                }
                else
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeYear);
                }
                break;
            case KeyboardAction.RIGHT:

                if(binding.tvKeyboardTimeYear.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeMonth);
                }
                else  if(binding.tvKeyboardTimeMonth.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeDay);
                }
                else  if(binding.tvKeyboardTimeDay.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeHour);
                }
                else  if(binding.tvKeyboardTimeHour.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeMinute);
                }
                else  if(binding.tvKeyboardTimeMinute.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeSecond);
                }
                else  if(binding.tvKeyboardTimeSecond.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.btnTimeCancel);
                }
                else  if(binding.btnTimeCancel.equals(KeyboardTimeContext.getSelectedView()))
                {
                    KeyboardTimeContext.setSelectedView(binding.btnTimeSave);
                }
                else
                {
                    KeyboardTimeContext.setSelectedView(binding.tvKeyboardTimeYear);
                }

                break;
        }
    }

    private void resetData()
    {
        binding.tvKeyboardTimeYear.setText(String.valueOf(KeyboardTimeContext.year));
        binding.tvKeyboardTimeMonth.setText(String.valueOf(KeyboardTimeContext.month));
        binding.tvKeyboardTimeDay.setText(String.valueOf(KeyboardTimeContext.day));
        binding.tvKeyboardTimeHour.setText(String.valueOf(KeyboardTimeContext.hour));
        binding.tvKeyboardTimeMinute.setText(String.valueOf(KeyboardTimeContext.minute));
        binding.tvKeyboardTimeSecond.setText(String.valueOf(KeyboardTimeContext.second));

    }
}
